package com.googlecode.vijayan.dogen;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 
 * @author Vijayan Srinivasan
 * @since Sep 4, 2013 8:18:48 PM
 *
 */
public class Table {
	private String name;
	private List<Column> columns;
	private String primaryKey;

	public Table() {
		this.columns = new ArrayList<Column>();
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Column> getColumns() {
		return this.columns;
	}

	public void setColumns(List<Column> columns) {
		this.columns = columns;
	}

	public String getPrimaryKey() {
		return this.primaryKey;
	}

	public void setPrimaryKey(String primaryKey) {
		this.primaryKey = primaryKey;
	}

	public String getEntityName() {
		String temp = this.name.toLowerCase();
		temp = toSingular(temp);

		StringBuffer className = new StringBuffer();
		char prevChar = ' ';

		for (int i = 0; i < temp.length(); ++i) {
			char c = temp.charAt(i);
			if ((i == 0) || (prevChar == '_')) {
				c = Character.toUpperCase(c);
			}
			if (c != '_') {
				className.append(c);
			}
			prevChar = c;
		}

		return className.toString();
	}

	public String getRepositoryName() {
		return new StringBuilder().append(getEntityName()).append("Repository")
				.toString();
	}

	private String toSingular(String temp) {
		if (temp.endsWith("ies"))
			temp = new StringBuilder()
					.append(temp.substring(0, temp.length() - 3)).append("y")
					.toString();
		else if (temp.endsWith("s")) {
			temp = temp.substring(0, temp.length() - 1);
		}
		return temp;
	}

	public Column getPrimaryKeyColumn() {
		for (Column column : columns) {
			if (column.getName().equalsIgnoreCase(this.primaryKey)) {
				return column;
			}
		}
		return null;
	}

	public String getAllColumnNames() {
		StringBuilder sb = new StringBuilder();
		Iterator<Column> it = columns.iterator();
		while (it.hasNext()) {
			Column column = (Column) it.next();
			sb.append(column.getName());
			if (it.hasNext()) {
				sb.append(",");
			}
		}
		return sb.toString();
	}

	public String getAllColumnNamesPlaceHolder() {
		StringBuilder sb = new StringBuilder();
		Iterator<Column>  it = columns.iterator();
		while (it.hasNext()) {
			it.next();
			sb.append("?");
			if (it.hasNext()){
				sb.append(",");
			}
		}

		return sb.toString();
	}
	
	public List<Column> getColumnsNoPk(){
		List<Column> columnsNoPk = new ArrayList<Column>(columns);
		Column pkColumn = getPrimaryKeyColumn();
		columnsNoPk.remove(pkColumn);
		return columnsNoPk;
	}

	public String getAllColumnNamesNoPk() {
		StringBuilder sb = new StringBuilder();
		Iterator<Column> it = columns.iterator();
		while (it.hasNext()) {
			Column column = (Column) it.next();
			if (!(column.getName().equalsIgnoreCase(primaryKey))) {
				sb.append(column.getName());
				if (it.hasNext()) {
					sb.append(",");
				}
			}
		}
		return sb.toString();
	}

	public String getAllColumnNamesNoPkPlaceHolder() {
		StringBuilder sb = new StringBuilder();
		Iterator<Column> it = columns.iterator();
		while (it.hasNext()) {
			Column column = (Column) it.next();
			if (!(column.getName().equalsIgnoreCase(primaryKey))) {
				sb.append("?");
				if (it.hasNext()) {
					sb.append(",");
				}
			}
		}
		return sb.toString();
	}
	
	public boolean hasPk(){
		return primaryKey != null;
	}
	
	public boolean isPkNameId(){
		return "id".equalsIgnoreCase(primaryKey);
	}
}
